package com.supermap.application.service;

import com.alibaba.fastjson.JSONObject;
import com.supermap.application.common.UserDepartmentFromKC;
import com.supermap.application.common.UserInfo;
import com.supermap.application.dao.UserDao;
import com.supermap.application.entity.User;
import com.supermap.application.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by sun on 2018/2/15.
 * User业务逻辑
 */
@Service
public class UserService {

    @Value("${sgis-address}")
    private String SGISaddress;

    //用户角色
    @Value("${sgis-role-id}")
    private String roleid;

    //用户类型
    @Value("${sgis-sys-role}")
    private String sysrole;

    @Autowired
    private UserDao userDao;


    public String getDepartName(UserInfo userInfo){
        Set<UserDepartmentFromKC> userDepartmentFromKCS = userInfo.getOrgs();
        String result = "";
        for (UserDepartmentFromKC userDepartmentFromKC : userDepartmentFromKCS) {
            String orgname = userDepartmentFromKC.getOrgname();
            result = result + orgname + ",";
        }
        return result;
    }

    /**
     * 向SGIS添加用户
     * @param userInfo
     * @return
     */
    public JSONObject addUserToSGIS(UserInfo userInfo){
        JSONObject result = new JSONObject();

        Map<String, Object> map = new HashMap<>();

        String departName = getDepartName(userInfo);

        String data = "{\"status\":\""+ userInfo.getState() +"\",\"userName\":\"" +userInfo.getName() + "\",\"userCaption\":\"\",\"password\":\"123456\",\"passwordConfirm\":\"123456\",\"majors\":\"\",\"userRegion\":\"湘江新区@430100000000\",\"userPartment\":\"\",\"phone\":\"\",\"email\":\"\",\"sys_role\":\"2\",\"roleid\":\"18\",\"flagA\":\""+userInfo.getId()+"\"}";
        String url = SGISaddress + "/data//users?_method=POST&status=" +userInfo.getState() + "&userName=" + userInfo.getPhone() + "&userCaption=" + userInfo.getName() +"&password=123456&passwordConfirm=123456&majors=&userRegion=湘江新区@430100000000&userPartment="+departName+"&phone="+userInfo.getPhone()+"&email=&sys_role="+sysrole+"&roleid="+ roleid + "&flagA=" + userInfo.getId();
        System.out.println("添加用户地址：" + url);

        map.put("status",userInfo.getState());
        map.put("userName",userInfo.getPhone());
        map.put("userCaption",userInfo.getName());
        map.put("password","1123456");
        map.put("passwordConfirm","123456");
        map.put("majors","");
        map.put("userRegion","湘江新区@430100000000");


        map.put("userPartment",departName);
        map.put("phone",userInfo.getPhone());
        map.put("sys_role","2");
        map.put("email","");
        map.put("roleid","18");
        map.put("flagA",userInfo.getId());
        String s = HttpClientUtils.doAddPost(url, map);
        System.out.println("添加返回结果" + s);

        if (s == null || s.equals("")){
            result.put("msg","操作失败");
            result.put("code","1");
            return result;
        }

        JSONObject jsonObject = JSONObject.parseObject(s);
        System.out.println(jsonObject);


        if (jsonObject.get("msg").equals("添加成功！")){
            result.put("msg","操作成功");
            result.put("code","0");
        }else {
            result.put("msg","操作失败");
            result.put("code","1");
        }



        return result;
    }

    public JSONObject updataUserToSGIS(UserInfo userInfo){

        JSONObject result = new JSONObject();
        String getUrl = SGISaddress + "/data/users/get/by/flagA?flagA=" + userInfo.getId();
        String s = HttpClientUtils.doGet(getUrl);
        System.out.println("验证用户url：" + s);
        if (s == null || s.equals("")){
            JSONObject jsonObject = addUserToSGIS(userInfo);
            return jsonObject;
        }

        String departName = getDepartName(userInfo);

        JSONObject jsonObject = JSONObject.parseObject(s);
        String id = jsonObject.get("userid") + "";
        int userid = Integer.parseInt(id);
        String userName = jsonObject.get("userName") + "";
        String userCaption = jsonObject.get("userCaption") + "";
        String userRegion = jsonObject.get("userRegion") + "";
        String userPartment = jsonObject.get("userPartment") + "";
        String memo = jsonObject.get("memo") + "";
        String status = jsonObject.get("status") + "";
        int statusId = Integer.parseInt(status);
        String email = jsonObject.get("email") + "";
        String phone = jsonObject.get("phone") + "";
        String sys_role = jsonObject.get("sys_role") + "";
        int sys_roleId = Integer.parseInt(sys_role);
        String flagA = jsonObject.getString("flagA");

        String updataUrl = SGISaddress + "/data/users/" + userid+ "?_method=PUT";

        Map<String,Object> map = new HashMap<>();
        map.put("userid",userid);
        map.put("status",userInfo.getState());
        map.put("userName",userInfo.getPhone());
        map.put("userCaption",userInfo.getName());
        map.put("password","123456");
        map.put("majors","");
        map.put("userRegion",userRegion);
        map.put("userPartment",departName);
        map.put("phone",userInfo.getPhone());
        map.put("email",email);
        map.put("sys_role",sys_roleId);
        map.put("flagA",userInfo.getId());

        String s1 = HttpClientUtils.doUpdatePost(updataUrl, map);
        System.out.println("请求返回结果：" + s1);

        if (s1 == null || s1.equals("")){
            result.put("msg","操作失败");
            result.put("code","1");
            return result;
        }

        JSONObject jsonObject1 = JSONObject.parseObject(s1);
        System.out.println("格式化结果：" + jsonObject1);


        if (jsonObject1.get("msg").equals("操作成功")){
            result.put("msg","操作成功");
            result.put("code","0");
        }else {
            result.put("msg","操作失败");
            result.put("code","1");
        }
        return result;
    }

    public JSONObject deleteUserInSGIS(UserInfo userInfo){

        JSONObject result = new JSONObject();
        String getUrl = SGISaddress + "/data/users/get/by/flagA?flagA=" + userInfo.getId();
        String s = HttpClientUtils.doGet(getUrl);

        if (s == null || s.equals("")){
            result.put("msg","操作失败");
            result.put("code","1");
            return result;
        }

        JSONObject jsonObject = JSONObject.parseObject(s);
        String id = jsonObject.get("userid") + "";
        int userid = Integer.parseInt(id);

        String deleteUrl = SGISaddress + "/data/users/" +userid + "?_method=DELETE";
        String deleteData = "";
        String delete = HttpClientUtils.doDeletePost(deleteUrl, deleteData);
        JSONObject resultTemp = JSONObject.parseObject(delete);
        System.out.println(resultTemp);

        if (resultTemp.get("msg").equals("成功删除1个用户")){
            result.put("msg","操作成功");
            result.put("code","0");
        }else {
            result.put("msg","操作失败");
            result.put("code","1");
        }
        return result;
    }


    /**
     * 根据用户名查找用户信息
     */
    public User findUserByName(String username){
        User byUserName = this.userDao.findByUserName(username);
        return byUserName;
    }
}
